package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class ChangePhoneActivity extends BaseActivity {

    @BindView(R.id.changephonetext)
    TextView changephonetext;

    @BindView(R.id.changephoneyanzhe)
    EditText changephoneyanzhe;
    @BindView(R.id.changeyanzheng)
    SuperButton changeyanzheng;

    @BindView(R.id.changephonetonext)
    SuperButton changephonetonext;
    @BindView(R.id.changphonephone)
    TextView changphonephone;
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);
        ButterKnife.bind(this);
changphonephone.setText(UserManageBean.userBean.getUserPhone());
    }

    @OnClick({R.id.changephoneback, R.id.changephonequhao, R.id.changeyanzheng, R.id.changephonetonext})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.changephoneback:
                goBack();
                break;
            case R.id.changephonequhao:
                Intent intent = new Intent(ChangePhoneActivity.this, SelectquhaoActivity.class);
                startActivityForResult(intent, 10);
                break;
            case R.id.changeyanzheng:
                if (changephonetext.getText().toString().equals("+86") && StringTool.isxuniphone(changphonephone.getText().toString())) {
                    showToast("请输入正确手机号");
                } else if (StringTool.isEmpty(changphonephone.getText().toString())) {
                    showToast("请输入手机号");
                } else {
                    getShortMessage();
                }
                break;
            case R.id.changephonetonext:
                if (StringTool.isEmpty(changphonephone.getText().toString())) {
                    showToast("请输入手机号");
                    return;
                } else if (!"".equals(changephoneyanzhe.getText().toString())) {
                    checkedCode();
                } else {
                    showToast("请输入验证码");
                }
                break;
        }
    }

    //验证验证码
    private void checkedCode() {
        OkHttpUtils.post(HttpManager.IP + "/verification/person-checkedCode")

                .params("checkCode", changephoneyanzhe.getText().toString())
                .params("phoneId", StringTool.deletadd(changephonetext.getText().toString()) + changphonephone.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                Intent intent = new Intent(ChangePhoneActivity.this, BiaoqianActivity.class);
                                startActivityForResult(intent, 5);

                            } else if ("500".equals(jsonObject.getString("returnCode"))) {
                                showToast(jsonObject.getString("msg"));
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }


    //发验证码
    private void getShortMessage() {
        OkHttpUtils.post(HttpManager.IP + "/verification/sys-getShortMessage")

                .params("userMob", StringTool.deletadd(changephonetext.getText().toString()) + changphonephone.getText().toString())
                .params("type", StringTool.huoqugettype(changephonetext.getText().toString(), "update"))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                showToast("发送成功");
                                countDownTimer = new CountDownTimer(60000, 1000) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        changeyanzheng.setText("剩" + millisUntilFinished / 1000 + "秒");
                                        changeyanzheng.setClickable(false);
                                        changeyanzheng.setsSolidColor("#D8D8D8");
                                    }

                                    @Override
                                    public void onFinish() {
                                        changeyanzheng.setText("重新发送");
                                        changeyanzheng.setClickable(true);
                                        changeyanzheng.setsSolidColor("#f77583");
                                    }
                                };
                                countDownTimer.start();
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 5 && requestCode == 5) {
            setResult(10);
            goBack();
        } else if (requestCode == 10 && resultCode == 10) {
            changephonetext.setText(data.getStringExtra("countryNumber"));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
